//
//  TF1to16VoteDetail.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/12.
//  Copyright © 2022 Talkfun. All rights reserved.
//
//TODO: 执行投票事件
//block的第一个参数是：问题的唯一标识vid    第二个参数是：  选项
typealias TF1to16VoteDetail_newVoteBlock = (_ vid:String,_ optionsArray:NSMutableArray) -> Void

import UIKit
import SDWebImage
class TF1to16VoteDetail: UIView {
@objc let back = TalkfunIncreaseButton()
    //用于判断是不是自己
    @objc var uid:String = ""
    @objc var newVoteBlock:TF1to16VoteDetail_newVoteBlock?

    let scrollView = UIScrollView()
    let voteDetailLabel = UILabel()
    //所有投票选项
    let contentImageViewArray = NSMutableArray()
    let manager = SDWebImageManager()
    //1 是单选 其它是多
    var optional:NSInteger = 1
    //唯一标识符
    @objc var vid:String = ""
    //投票详情
    @objc var obj:TFVoteData?

    {
        didSet{
            if (obj != nil)
            {
                
            }
        }
    }
    var image = UIImage()
    let fullScreen = UIView()
    //无投票内容
    let votingContentLabel = UILabel()
    let headView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
  
        self.addSubview(self.scrollView)
    
        
        self.headView.backgroundColor = UIColor(red: 47/255.0, green: 52/255.0 , blue: 59/255.0, alpha: 1)
        self.addSubview(self.headView)
        
   
        
        self.voteDetailLabel.numberOfLines = 0
        self.headView.addSubview(voteDetailLabel)
        
        
        let string = NSMutableAttributedString.getAttributed(string: "投票详情" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
        
        self.voteDetailLabel.attributedText = string;
        self.voteDetailLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
      

        self.back.setImage(UIImage.init(named: "HTLive.bundle/back_1.png"), for: .normal)
        self.back.backgroundColor = UIColor.clear
        self.headView.addSubview(self.back)
        

        //投票内容
  
        self.votingContentLabel.numberOfLines = 0
        self.addSubview(self.votingContentLabel)
        
        let votingContentLabelString = NSMutableAttributedString.getAttributed(string: "无投票内容" , font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0, alpha: 1))
      
        self.votingContentLabel.attributedText = votingContentLabelString
        self.votingContentLabel.textColor = UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0, alpha: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.headView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)

        let  voteDetailLabelW:CGFloat = 100;
        let  voteDetailLabelH:CGFloat = 24;
        let  voteDetailLabelX:CGFloat = 41;
        let  voteDetailLabelY:CGFloat = 10;
        self.voteDetailLabel.frame = CGRect(x: voteDetailLabelX, y: voteDetailLabelY, width: voteDetailLabelW, height: voteDetailLabelH)
        
         self.back.frame = CGRect(x: 10, y: voteDetailLabelY, width: 24, height: 24)
      
        
         self.scrollView.frame = CGRect(x: 0, y: 45, width: self.frame.size.width, height: self.frame.size.height - 30)
        
         self.votingContentLabel.frame = CGRect(x: (self.frame.size.width-90)/2, y: (self.frame.size.height-25)/2, width: 90, height: 25)
       
    }
    


//   // #pragma mark  投票的选择
    @objc func votingBehaviorClick()
    {
        let option = NSMutableArray()
      
        for i in 0..<self.contentImageViewArray.count {
            let btn = self.contentImageViewArray[i] as! TalkfunIncreaseButton
          
            if (btn.isSelected) {
                option.add(String.init(format: "%i", btn.tag))
            }
         
        }
        
        if (option.count>0) {
            if(self.newVoteBlock != nil){
                self.newVoteBlock!(self.vid, option)
            }
            
        }
        
    }
//    //#pragma mark 投票成功,更新本地数据
    @objc func successfulVote()
    {
        let option = NSMutableArray()
        
        //投票的选项要加上
        for i in 0..<self.contentImageViewArray.count {
            let btn = self.contentImageViewArray[i] as! TalkfunIncreaseButton

            if (btn.isSelected) {
                option.add(String.init(format: "%i", btn.tag))
               
            }
            
          }
        
        
        let user = TFVoteNewUser()
        user.uid = self.uid
        user.isMe = true
        user.option = option
        if self.obj != nil {
            
            self.obj!.userList.add(user)
            //刷新假数据
            self.updateVoteDetail(self.obj!)
        }
    }
//    //#pragma mark  投票选项的选择
    @objc func contentImageViewClick(contentImageView:TalkfunIncreaseButton)
 
    {
        contentImageView.isSelected =  !contentImageView.isSelected;
        if (contentImageView.isSelected) {
            contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulegou"), for: .normal)
            
        }else{
            contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulebugou"), for: .normal)
           
        }
        
        //  1单选    其它多选
        if (self.optional==1) {
            for i in 0..<self.contentImageViewArray.count {
                let btn = self.contentImageViewArray[i] as! TalkfunIncreaseButton
          
                
                if (contentImageView.tag != btn.tag) {
                    
                    btn.setImage(UIImage.init(named: "HTLive.bundle/bulebugou"), for: .normal)
                    btn.isSelected = false
                }
            }
        }
        
    }
    
    @objc func backClick()

    {
        self.isHidden = true
        if self.scrollView.subviews.count>0{
                    self.scrollView.subviews.forEach({$0.removeFromSuperview()})
        }
        self.contentImageViewArray.removeAllObjects()
  
    }
    //全屏
    @objc func imageButtonClick()
    {
        
        let window = TalkfunSharedTools.keyWindow()
        
        if window != nil {
            self.fullScreen.backgroundColor = UIColor.clear
            self.fullScreen.frame = window.frame
            window.addSubview(self.fullScreen)
            
            

            let browseView = UIImageView.init(frame: self.fullScreen.frame)
            browseView.backgroundColor = UIColor(red: 21/255.0, green: 34/255.0, blue: 54/255.0, alpha: 0.95)
            browseView.contentMode = .scaleAspectFit
            browseView.image = self.image
            
            
            fullScreen.addSubview(browseView)
           
            
            let imageView = UIButton()
            imageView.contentMode = .scaleAspectFit
            imageView.setImage(UIImage.init(named: "HTLive.bundle/suoxiao"), for: .normal)
            
            imageView.frame = CGRect(x: fullScreen.frame.size.width-50-25, y: fullScreen.frame.size.height-50-10, width: 50, height: 50)
            
            imageView.backgroundColor = UIColor(red: 29/255.0, green: 56/255.0, blue: 83/255.0, alpha: 0.6)
            imageView.layer.cornerRadius = 25
            imageView.addTarget(self, action: #selector(self.imageViewClick), for: .touchUpInside)
            fullScreen.addSubview(imageView)
        }
    
    }
    //缩小
    @objc func imageViewClick()
    {
        self.fullScreen.removeFromSuperview()
    }
    @objc func upDataLanguage()
    {
        
        let string = NSMutableAttributedString.getAttributed(string: "投票详情", font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
        
        self.voteDetailLabel.attributedText = string
    }
    //删除最后一个字符
    func removeLastOneChar(_ origin:NSString)->String
    {
        var cutted:String = ""
        if(origin.length > 0){
          //   去掉最后一个","
            let count:NSInteger = NSInteger(origin.length-1)
            cutted = origin.substring(to: count) as String
        }else{
            cutted = origin  as String
        }
        return cutted;
    }
   @objc func updateVoteDetail(_ obj:TFVoteData)
    
    {
//清空数据
      if self.scrollView.subviews.count>0{
          self.scrollView.subviews.forEach({$0.removeFromSuperview()})
      }
      self.contentImageViewArray.removeAllObjects()
      self.obj = nil
          
          
          self.obj = obj
          self.isHidden = false

          self.votingContentLabel.isHidden = true
          
          let dividingLine = UIView()
          dividingLine.backgroundColor = UIColor(red: 53/255.0, green: 56/255.0, blue: 59/255.0, alpha: 1)
          dividingLine.layer.cornerRadius = 3
      self.scrollView.addSubview(dividingLine)
          dividingLine.frame = CGRect(x: 18.5, y: 0, width: self.frame.size.width-(18.5*2), height: 0.4)
          //TODO: 用户名字
          let nickname:NSString = obj.info.nickname as NSString
          //TODO: 投票结束时间
          let info_endTime:NSString = obj.info.endTime as NSString
          var endTime:Bool = false
       
          if info_endTime == "0" {
             // 投票未结束 endTime = 0
             endTime = false
          }else
           {
              //投票的结束  endTime == 2021-03-24 16:13:09
              endTime = true
          }
          //TODO: 投票开始时间
          let startTime = obj.info.startTime as NSString
          //TODO: 标签   (有数据就是有标签)
          let label = obj.info.label as NSString

          //TODO: 主题是图片  (有数据就是有标签)
          let imageUrl = obj.info.imageUrl as NSString
          //TODO: 主题的标题   (有数据就是有标签)
          let title = obj.info.title as NSString
          //TODO:  1单选     不等于1 就是多选
          let optional:NSInteger = obj.optional
          //TODO: 正确答案是
          let answer = obj.info.answer as NSArray

          self.optional = optional
          self.vid = obj.vid
          //TODO: 投票选项 a b c
          let opList = obj.opList as NSArray
          //TODO: 0 未投票   1 已投票
          var voted:Bool = false
         //已投票的用户列表
          if (obj.userList.count>0) {
              for i in 0..<obj.userList.count {
                  let uidDict = obj.userList[i] as! TFVoteNewUser
             
                  //是自己
                  if (uidDict.isMe) {
                      voted = true
                  }
              }
          }
          //TODO:投票人发起人的名字
          let nicknameLabel = UILabel()
          nicknameLabel.frame =  CGRect(x: 20, y: 15, width: 70, height: 20)
          nicknameLabel.numberOfLines = 0
      
        let nicknameString = NSMutableAttributedString.getAttributed(string: nickname as String, font: UIFont.PingFang(size: 15), foregroundColor: UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1))
          
          nicknameLabel.attributedText = nicknameString
          nicknameLabel.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)

          
          //TODO:投票状态  (投票中  已结束  已投票)
      let votingStatus   = UILabel()
      let  votingStatusW:CGFloat = 42
      let  votingStatusH:CGFloat = 20
      let  votingStatusX:CGFloat = self.frame.size.width - votingStatusW - 20
          votingStatus.frame = CGRect(x: votingStatusX, y: 15, width: votingStatusW, height: votingStatusH)
      self.scrollView.addSubview(votingStatus)
        
        
        //投票中
       if (endTime==false) {
           if(voted==false){
               
               votingStatus.attributedText = NSMutableAttributedString.getAttributed(string: "投票中" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 159/255.0, blue: 45/255.0, alpha: 1));
               
               votingStatus.backgroundColor = UIColor.clear
               
               votingStatus.textColor = UIColor(red: 255/255.0, green: 159/255.0, blue: 45/255.0, alpha: 1)
           }else{
               
             
               votingStatus.attributedText = NSMutableAttributedString.getAttributed(string: "已投票" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1))
               votingStatus.textColor = UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)
               
               votingStatus.backgroundColor = UIColor.clear
           }
           
       }
       else{
           
           votingStatus.attributedText = NSMutableAttributedString.getAttributed(string: "已结束" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1));
           votingStatus.textColor = UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)
           votingStatus.backgroundColor = UIColor.clear
           
       }
               //TODO:发起投票时间
               let startTimeLabel =  UILabel()
               startTimeLabel.frame = CGRect(x: nicknameLabel.frame.origin.x, y: nicknameLabel.frame.origin.y, width: self.frame.size.width-nicknameLabel.frame.origin.x, height: 20)
               startTimeLabel.numberOfLines = 0;
               self.scrollView.addSubview(startTimeLabel)
           
               let sonderVote = "发起投票"
           
           
           let str = (nickname as String)+((" " as NSString) as String)+(startTime as String)+sonderVote
           
               
        let startTimeLabelString = NSMutableAttributedString.getAttributed(string: str, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1))
           
               startTimeLabel.attributedText = startTimeLabelString
               startTimeLabel.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
          
               //TODO:标签
               let  bq1 = UILabel()
               bq1.text = "标签"
               bq1.font = UIFont.PingFang(size: 14)
               bq1.textColor = UIColor.white
               self.scrollView.addSubview(bq1)
               
               let Label = UILabel()
               var LabelY:CGFloat = startTimeLabel.frame.origin.y
               + startTimeLabel.frame.size.height
               let LabelX:CGFloat = startTimeLabel.frame.origin.x
               let LabelW:CGFloat = self.frame.size.width - LabelX
               var LabelH:CGFloat = 0
               //主题才有 标签
               if (label.length>0&&imageUrl.length>0) {
                   LabelH = 20
                   bq1.frame = CGRect(x: LabelX, y: startTimeLabel.frame.maxY+15, width: 100, height: 20)
                   
                   LabelY = bq1.frame.maxY+7
               }

               Label.frame = CGRect(x: LabelX, y: LabelY, width: LabelW, height: LabelH)

               Label.numberOfLines = 0
               Label.text = label as String
               Label.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
           
               Label.font = UIFont.PingFang(size: 14)
               self.scrollView.addSubview(Label)
              
               
               //TODO:主题
               let titleLabel = UILabel()
               let titleLabelX:CGFloat = Label.frame.origin.x
               let titleLabelY:CGFloat = Label.frame.origin.y + Label.frame.size.height + 20;
               let  titleLabelW:CGFloat = 42
               let  titleLabelH:CGFloat = 20;
               titleLabel.frame = CGRect(x: titleLabelX, y: titleLabelY, width: titleLabelW, height: titleLabelH)
               titleLabel.numberOfLines = 0
           self.scrollView.addSubview(titleLabel)
           
        let titleLabelString = NSMutableAttributedString.getAttributed(string: "主题" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
           
               titleLabel.attributedText = titleLabelString

           let theme = UIView()
           theme.backgroundColor = UIColor.clear
           self.scrollView.addSubview(theme)
               //33 是右间距
           let themeX:CGFloat = titleLabel.frame.origin.x;
           let themeY:CGFloat = titleLabel.frame.maxY+7
               //TODO: 主题是图片
               if(imageUrl.length>0)
               {
                   let themeW:CGFloat = self.frame.size.width - titleLabel.frame.size.width - titleLabel.frame.origin.x - 33;
                   theme.frame = CGRect(x: themeX, y: themeY, width: themeW, height: 104)
                   self.manager.loadImage(with: NSURL.init(string: imageUrl as String) as URL?) { receivedSize, expectedSize, targetURL in
                       
                   } completed: { image, data, error, cacheType, finished, imageURL in
                       
           
                       if (image != nil) {
                           
                           self.image = image!
                           let imageButton = TFZoomButton()
                           imageButton.imageView!.contentMode = .scaleToFill
                           imageButton.setImage(image, for: .normal)
                           
                           imageButton.frame = CGRect(x: 0, y: 0, width: theme.frame.size.width, height: theme.frame.size.height)
                           
                           theme.addSubview(imageButton)
                           imageButton.addTarget(self, action: #selector(self.imageButtonClick), for: .touchUpInside)
                          
          
                       }
                       
                   }
                   
                   
               }
               //TODO:主题是文字
               else if (title.length>0)
               {
                   let themeW:CGFloat = self.frame.size.width-40

                   
                   let rect = TalkfunSharedTools.getSizeLabel(labelString: NSMutableAttributedString.init(string: title as String), labelWidth: themeW, labelfont: UIFont.PingFang(size: 14))
                   
                   theme.frame = CGRect(x: themeX, y: themeY, width: themeW, height: rect.height)
                 
                   let msg = UITextView()
                   msg.isUserInteractionEnabled = false
                   let msgX:CGFloat = 0;
                   let msgY:CGFloat = 0;
                   let msgW:CGFloat = themeW
                   let msgH:CGFloat = rect.height
                   msg.frame = CGRect(x: msgX, y: msgY, width: msgW, height: msgH)
                 
                   titleLabel.numberOfLines = 0
                   theme.addSubview(msg)
                 
                   
                   let msgString = NSMutableAttributedString.getAttributed(string: title as String, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
                   
                   msg.attributedText = msgString
           
                   
                   //内容缩进为0（去除左右边距）
                   msg.textContainer.lineFragmentPadding = 0
                   //文）
                   msg.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
         
                   msg.backgroundColor = UIColor.clear
                   
               }
           let optionalLabelX:CGFloat = titleLabel.frame.origin.x
           let optionalLabelY:CGFloat = theme.frame.origin.y+theme.frame.size.height + 21;
           let optionalLabelW:CGFloat = 126
           let optionalLabelH:CGFloat = 20
               //TODO: 单选 多选
               let optionalLabel = UILabel()
               optionalLabel.frame = CGRect(x: optionalLabelX, y: optionalLabelY, width: optionalLabelW, height: optionalLabelH)
               optionalLabel.numberOfLines = 0
           self.scrollView.addSubview(optionalLabel)
           var optionalLabelMsg:String = ""
               //单选   //  "optional" : 1     不等于1 就是多选
               if (optional==1) {
                   
                   optionalLabelMsg = "选票选项（单选)"
               }else{
                   
                   optionalLabelMsg = "选票选项（多选)"
               }
               
        let optionalLabelString = NSMutableAttributedString.getAttributed(string: optionalLabelMsg , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
               
               optionalLabel.attributedText = optionalLabelString
           
               let copyStatsList = NSArray.init(array: opList)
               
               //底部选项的Y. 加上高度
              var  YOfTheBottomOption:CGFloat = 0;
               //清空所有
              self.contentImageViewArray.removeAllObjects()
              
               
              let vcX:CGFloat = 20
               //自己的选项
              let option = NSMutableArray()
              for i in 0..<obj.userList.count {
               let uidDict = obj.userList[i] as! TFVoteNewUser
    
                   //是自己
                   if (uidDict.isMe) {
                       option.addObjects(from: uidDict.option as! [Any])
                
                   }
                }
           
        //TODO: 选项 A B C
        for i in 0..<opList.count {
     
            let dict = opList[i] as! TFOp
            
            //百分比
            let percent = dict.percent as NSString
            //投了多少票
            let opNum = dict.opNum as NSString
            //选项内容
            
            
            
            let content  = NSString.init(format: "%@ %@", TalkfunSharedTools.asciiBytesToString(inx: NSInteger(i)),dict.op)
            
            let contentX:CGFloat = 53
            
            var hegiht:CGFloat = 0
            //第二排查
            if (i>0) {
                for j in stride(from: 0, to: copyStatsList.count  ,by: 1) {
             
                    if j < i {
                        let dict = copyStatsList[j] as! TFOp
                        //选项内容
                       let content  = NSString.init(format: "%@ %@", TalkfunSharedTools.asciiBytesToString(inx: NSInteger(i)),dict.op)
                        
                        //53是左间距   22是右间距
                        let contentW:CGFloat = self.frame.size.width - contentX - 20;
                        //计算内容高度
                       
                        
                        let rect  =  TalkfunSharedTools.getSizeLabel(labelString: NSMutableAttributedString.init(string: content as String), labelWidth: contentW, labelfont: UIFont.PingFang(size: 14))
                          hegiht =  hegiht + rect.height
                        
                        //TODO:  投票已经结束   并公布了答案
                        if ( endTime && obj.isShow) {
                            
                            hegiht = hegiht + 20 + 19;
                            
                        }
                    }
                  
                }
                
            }
     
            let contentY = optionalLabelY + optionalLabelH + (16*(CGFloat(i)+1) + hegiht );
            
            //53是左间距   22是右间距
            let contentW = self.frame.size.width - contentX - 20
            //计算内容高度
            
            
            
            let rect = TalkfunSharedTools.getSizeLabel(labelString: NSMutableAttributedString.init(string: content as String), labelWidth: contentW, labelfont: UIFont.PingFang(size: 14))
            
            //选项
            let vc = UIView()
            vc.backgroundColor = UIColor.clear
            vc.frame = CGRect(x: vcX, y: contentY, width: self.frame.size.width - vcX - vcX, height: rect.height)
    
            self.scrollView.addSubview(vc)
        
            var percentageHeight:CGFloat = 0
            
          
            //TODO:  投票已经结束   并公布了答案
            if (endTime&&obj.isShow) {
                vc.frame = CGRect(x: vcX, y: contentY, width: self.frame.size.width - vcX - vcX, height: rect.height+19)
                
                
//                var percentageHeight:CGFloat = 20
                
                percentageHeight = 20;
                //选项
                let percentage = UIView()
                percentage.backgroundColor = UIColor.clear
               
                //22 是左边间距    36 是 vcX +7
                let percentageX:CGFloat = 36+22; //36 +22
                percentage.frame = CGRect(x: percentageX, y: contentY + vc.frame.size.height + 7, width: contentW, height: percentageHeight)
               
                self.scrollView.addSubview(percentage)
                
                percentageHeight = percentageHeight + 7
                
                let  pi = "票"
                
                
                let percentageText =  (opNum as String) + pi + "(" + (percent as String) + "%)"
             
                //文本宽度
            
                let attrsSize: CGSize = percentageText.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
                
        
                let percentageLabelWidth:CGFloat  = attrsSize.width
                let percentageLabelX:CGFloat = percentage.frame.size.width - percentageLabelWidth;
                
                let percentageLabel = UITextView()
                
                percentageLabel.frame = CGRect(x: percentageLabelX, y: 0, width: percentageLabelWidth, height: percentageHeight)
                
                percentage.addSubview(percentageLabel)
            
                
                let percentageString = NSMutableAttributedString.getAttributed(string: percentageText, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
              
                percentageLabel.attributedText = percentageString
                percentageLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
          
                percentageLabel.textContainer.lineFragmentPadding = 0
                percentageLabel.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
           
                percentageLabel.backgroundColor = UIColor.clear
                percentageLabel.textContainer.maximumNumberOfLines = 1
                percentageLabel.textContainer.lineBreakMode = .byTruncatingTail;
                
                //百份比ui
                let percentageView = UIView()

                percentageView.frame = CGRect(x: 0, y: 0, width:  vc.frame.size.width, height:  vc.frame.size.height)
               
                percentageView.backgroundColor = UIColor(red: 39/255.0, green: 44/255.0, blue: 48/255.0, alpha: 1)
            
                percentageView.layer.cornerRadius = 4
                vc.addSubview(percentageView)
                
                
                //蓝色比例
                let blueview = UIView()
               
                let blueviewW:CGFloat = percentageView.frame.size.width * CGFloat(percent.floatValue)/100;

                blueview.frame = CGRect(x: 0, y: 0, width: blueviewW, height: vc.frame.size.height)
               
           
                blueview.backgroundColor = UIColor(red: 75/255.0 , green: 82/255.0, blue: 93/255.0, alpha: 1)
              
                vc.addSubview(blueview)
       
                
                let maskeight:CGFloat = 4.0
                
                let maskPath = UIBezierPath.init(roundedRect: blueview.bounds, byRoundingCorners: [.topLeft,.bottomLeft], cornerRadii: CGSize(width: maskeight, height: maskeight))
              
                           
               let   maskLayer = CAShapeLayer()
               maskLayer.frame = blueview.bounds
               maskLayer.path = maskPath.cgPath
                           
               blueview.layer.mask = maskLayer
                
                
            } else {
               
                let myString = "我是用来计算高度的"
                let attrsSize: CGSize = myString.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])

                //不勾
                let contentImageView = TalkfunIncreaseButton.init(frame: CGRect(x: 0, y: 0, width: attrsSize.height, height: attrsSize.height))
                contentImageView.backgroundColor = UIColor.clear
                contentImageView.tag = i
                contentImageView.addTarget(self, action: #selector(self.contentImageViewClick), for: .touchUpInside)
                vc.addSubview(contentImageView)
                self.contentImageViewArray.add(contentImageView)
              
            }

            //最后一个选项的Y加H
            YOfTheBottomOption = vc.frame.origin.y + vc.frame.height + percentageHeight
            
            let  contentLabel  = UITextView()
            
            if ( endTime && obj.isShow) {
                contentLabel.frame = CGRect(x: 10, y: 9.5, width: vc.frame.size.width-10, height: vc.frame.size.height-19)
                
            } else {
                contentLabel.frame = CGRect(x: vcX+14, y: 0, width: vc.frame.size.width - vcX - 14, height: vc.frame.size.height)
                
            }
            vc.addSubview(contentLabel)
            
            
            contentLabel.isUserInteractionEnabled = false
            
            
            let contentString = NSMutableAttributedString.getAttributed(string: content as String, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
            contentLabel.attributedText = contentString
            contentLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)

            //内容缩进为0（去除左右边距）
            contentLabel.textContainer.lineFragmentPadding = 0
            //文）
            contentLabel.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0 )
        
            contentLabel.backgroundColor = UIColor.clear

            
        }
        
        for i in 0..<self.contentImageViewArray.count {
     
            let contentImageView = self.contentImageViewArray[i] as! TalkfunIncreaseButton
      
        
            //但是未投票
            if(voted == false  && endTime==false ){
                contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulebugou"), for: .normal)
               
            }else{
                
                //已经有人投票   本人已投票
                
                if((obj.userList.count>0&&option.count>0)  || obj.isShow ){
                    
                    //证明我已经投票
                    contentImageView.isUserInteractionEnabled = false
                    contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulegoued"), for: .normal)
                    for i in 0..<option.count {
                        let optionNumber = option[i] as! NSString
                 
                        
                        //选择的选项
                        if (optionNumber.intValue>contentImageView.tag) {
                            contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulegou"), for: .normal)
                          
                        }
                    }
                    
                } else{
                    contentImageView.setImage(UIImage.init(named: "HTLive.bundle/bulebugou"), for: .normal)
                   
                }
                
            }
            
        }
        //TODO:但是未投票
        if(voted==false){
            if(endTime==false){
                //TODO:投票button
                let votingBehaviorY:CGFloat = YOfTheBottomOption + 33
                let votingBehaviorW:CGFloat = 100
                let votingBehaviorH:CGFloat = 35

                
                let votingBehaviorX:CGFloat = (self.scrollView.frame.size.width - votingBehaviorW) / 2
                let votingBehavior = UIButton()
                self.scrollView.addSubview(votingBehavior)
                votingBehavior.setTitle("投票", for: .normal)
                votingBehavior.addTarget(self, action: #selector(votingBehaviorClick), for: .touchUpInside)
               
                votingBehavior.titleLabel!.font = UIFont.PingFang(size: 14)
                votingBehavior.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1), for: .normal)
               
                
                votingBehavior.frame = CGRect(x: votingBehaviorX, y: votingBehaviorY, width: votingBehaviorW, height: votingBehaviorH)
                votingBehavior.backgroundColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
               
                votingBehavior.layer.cornerRadius = 3.0
                
                self.scrollView.contentSize = CGSize(width: self.frame.size.width, height: votingBehavior.frame.origin.y+votingBehavior.frame.size.height+33)
             
            }else{
                
                //显示投票
                
              let model1 =  self.getOptionAnswer(wrongAnswer: "你的选项：", option: option)
                let wrongAnswer = model1.wrongAnswer
             
                let optionAnswer1 = model1.optionAnswer
               
                
                //TODO: 你的选项是:
                let yourAnswerY:CGFloat = YOfTheBottomOption + 20 + 26;
                let yourAnswerX:CGFloat = vcX ;
                let yourAnswerW:CGFloat = self.scrollView.frame.size.width - vcX - vcX
                let yourAnswerH:CGFloat = 20
                
                let yourAnswer = UILabel()
                
                yourAnswer.frame = CGRect(x: yourAnswerX, y: yourAnswerY, width: yourAnswerW, height: yourAnswerH)
                yourAnswer.numberOfLines = 0
                
                let yourAnswerString = NSMutableAttributedString.getAttributed(string: wrongAnswer , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
                
                yourAnswer.attributedText = yourAnswerString
                yourAnswer.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
                self.scrollView.addSubview(yourAnswer)
                
                
                
                //TODO: 正解的选项是:
                let normalAnswerX:CGFloat = yourAnswerX
                let normalAnswerY:CGFloat = yourAnswerY + yourAnswerH + 4;
                let normalAnswerW:CGFloat = yourAnswerW
                let normalAnswerH:CGFloat = yourAnswerH
                
                let normalAnswer = UILabel()
                normalAnswer.frame = CGRect(x: normalAnswerX, y: normalAnswerY, width: normalAnswerW, height: normalAnswerH)
                normalAnswer.numberOfLines = 0
                self.scrollView.addSubview(normalAnswer)
                
                
                
                let model2 =  self.getOptionAnswer(wrongAnswer: "正确答案：", option: answer)
                
                let optionAnswer2 = model2.optionAnswer
                //正确答案:a,b
                let answerStr = model2.wrongAnswer
               
                
                var normalAnswerString = NSMutableAttributedString()
                
                //选项相同 答案正确
                if (optionAnswer1 == optionAnswer2 && optionAnswer1.count>0) {
                    
                    
                    normalAnswerString = NSMutableAttributedString.getAttributed(string: "正确答案：", font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 23/255.0, green: 243/255.0, blue: 251/255.0, alpha: 1))
                    
                }else{
                    normalAnswerString = NSMutableAttributedString.getAttributed(string: answerStr , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 23/255.0, green: 243/255.0, blue: 251/255.0, alpha: 1))
                    
                    
                }
                
                
                
                
                normalAnswer.attributedText = normalAnswerString
                normalAnswer.alpha = 1.0;
                
                self.scrollView.contentSize = CGSize(width: self.frame.size.width, height: normalAnswer.frame.origin.y + normalAnswer.frame.size.height + 33)
               
            }
           
        }
        else{
            
               //TODO:显示答案
                //已经投票了
            
            
            let model3 = self.getOptionAnswer(wrongAnswer: "你的选项：", option: option)
  
            //你的选项是 //a.b
            let optionAnswer3 = model3.optionAnswer
            let wrongAnswer = model3.wrongAnswer
            
    
                //TODO: 你的选项是:
            var yourAnswerY:CGFloat = YOfTheBottomOption + 26
                if (obj.isShow) {
                    yourAnswerY = YOfTheBottomOption + 20 + 26
                }
            let yourAnswerX:CGFloat = vcX
            let yourAnswerW:CGFloat = self.scrollView.frame.size.width - vcX - vcX
            let yourAnswerH:CGFloat = 20
            let yourAnswer = UILabel()
                
            yourAnswer.frame = CGRect(x: yourAnswerX, y: yourAnswerY, width: yourAnswerW, height: yourAnswerH)
           
                yourAnswer.numberOfLines = 0
                
            
            
            let yourAnswerString = NSMutableAttributedString.getAttributed(string:wrongAnswer , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
                
                yourAnswer.attributedText = yourAnswerString
                yourAnswer.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
            self.scrollView.addSubview(yourAnswer)
            self.scrollView.contentSize = CGSize(width: self.frame.size.width, height: yourAnswer.frame.origin.y + yourAnswer.frame.size.height + 33)
       
                
                if (obj.isShow) {
                    //TODO: 正解的选项是:
                    let normalAnswerX:CGFloat = yourAnswerX;
                    let normalAnswerY:CGFloat = yourAnswerY + yourAnswerH + 4;
                    let normalAnswerW:CGFloat = yourAnswerW;
                    let normalAnswerH:CGFloat = yourAnswerH;
                    
                    let normalAnswer = UILabel()
                    normalAnswer.frame = CGRect(x: normalAnswerX, y: normalAnswerY, width: normalAnswerW, height: normalAnswerH)
                    normalAnswer.numberOfLines = 0
                    self.scrollView.addSubview(normalAnswer)
                    
                    //正确的选项是
                    let model4 =  self.getOptionAnswer(wrongAnswer: "正确答案：", option: answer)
                    //a.b
                    let optionAnswer4 = model4.optionAnswer
                    //正确的选项 //a.b
                    let answerStr = model4.wrongAnswer
                    
                    var normalAnswerString = NSMutableAttributedString()
                    
                    //选项相同 答案正确
                    if (optionAnswer3 == optionAnswer4 && optionAnswer3.count>0) {
                 
                        
                        normalAnswerString = NSMutableAttributedString.getAttributed(string: "回答正确" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 23/255.0, green: 243/255.0, blue: 251/255.0, alpha: 1))
                        
                    }else{
                        normalAnswerString = NSMutableAttributedString.getAttributed(string: answerStr, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 23/255.0, green: 243/255.0, blue: 251/255.0, alpha: 1))
                        
                       
                    }
                    
                    
                    normalAnswer.attributedText = normalAnswerString
                    normalAnswer.alpha = 1.0
                    
                    self.scrollView.contentSize = CGSize(width: self.frame.size.width, height: normalAnswer.frame.origin.y + normalAnswer.frame.size.height + 33)
                   
                }

            
        }
        self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        
       
    }
    func getOptionAnswer(wrongAnswer:String,option:NSArray)->optionModel
    {
        var optionAnswer:String = ""
        
        //你的选项:a,b
        var newMsg:String = wrongAnswer
        if ( option.count > 0 ) {
            
            if ( option.count > 1 ) {
                
                for i in 0..<option.count {
      
                    let g:NSString = option[i] as! NSString
                   
                    optionAnswer = (optionAnswer as String)+TalkfunSharedTools.asciiBytesToString(inx: g.integerValue)
                   
                    optionAnswer = optionAnswer + "、"
                }
                optionAnswer = self.removeLastOneChar(optionAnswer as NSString)
                newMsg = newMsg + (optionAnswer as String)
                //    NSLog(@"选择的答案是: %@",ggg);
            }else{
                if(option.count > 0){
                    let g:NSString = option[0] as! NSString
                    //单项
                    optionAnswer = TalkfunSharedTools.asciiBytesToString(inx: g.integerValue)
                    
                    newMsg = newMsg + (optionAnswer as String)
                    
                }
                
            }
            
        }
         let optionModel = optionModel()
        //你的选项:a,b
        optionModel.wrongAnswer = newMsg
        //a.b
        optionModel.optionAnswer = optionAnswer
        return optionModel
    }
    class optionModel: NSObject {
        //a.b
        var optionAnswer:String = ""
        //选项:a,b
        var wrongAnswer:String = ""
    }
    
}
